package com.chenqi.myblog.controller;

import com.chenqi.myblog.model.*;
import com.chenqi.myblog.service.BlogService;
import com.chenqi.myblog.service.CommentService;
import com.chenqi.myblog.service.TagService;
import com.chenqi.myblog.service.TypeService;
import com.chenqi.myblog.utils.MarkdownUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @program: myblog
 * @author: ChenQi
 * @create: 2021-02-2021/2/2-16-17
 */
@Controller
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @Autowired
    private CommentService commentService;

    @Value("${blog.page}")
    private Integer blogPage;

    @Value("${blog.rows}")
    private Integer blogRows;

    @Value("${newBlog.start}")
    private Integer newBlogStart;

    @Value("${newBlog.rows}")
    private Integer newBlogRows;

    /**
     * 首页分页显示博客列表
     * @param page
     * @param rows
     * @param modelAndView
     * @return
     */

    @GetMapping("/list")
    public ModelAndView findByPage(@RequestParam(value = "page",required = false) Integer page,
                                   @RequestParam(value = "rows",required = false) Integer rows,ModelAndView modelAndView) {
        //如果没有传分页信息则进行预处理
        page = page == null ? blogPage : page;
        rows = rows == null ? blogRows : rows;
        Integer counts = blogService.findCounts();
        List<Blog> blogList = blogService.findByPage(page, rows);
        Integer totalPages = counts % rows == 0 ? counts / rows : counts / rows + 1;
        List<Integer> integerList = new ArrayList<>();
        for (int i = 1; i <= totalPages; i++) {
            integerList.add(i);
        }
        //封装分页信息
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPage(page);
        pageInfo.setRows(rows);
        pageInfo.setCounts(counts);
        pageInfo.setBlogList(blogList);
        pageInfo.setTotalPages(totalPages);
        pageInfo.setPageList(integerList);
        modelAndView.addObject("pageInfo", pageInfo);
        //首页分类信息(默认从0开始查6个)
        List<Type> types = typeService.findSomeType(0, 6);
        List<Type> newTypes = new ArrayList<>();
        for (Type type:types) {
            Integer count = typeService.findTypeCountByBlog(type.getId());
            type.setTotal(count);
            newTypes.add(type);
        }
        //首页标签(默认查全部)
        List<Tag> tags = tagService.findAllTag();
        List<Tag> newTags = new ArrayList<>();
        for (Tag tag : tags) {
            Integer counts1 = tagService.findTagCounts(tag.getId());
            tag.setTotal(counts1);
            newTags.add(tag);
        }
        //首页最新博客推荐(倒叙推荐8个)
        List<Blog> newBlog = blogService.findNewBlog(newBlogStart, newBlogRows);
        modelAndView.addObject("newBlogs", newBlog);
        modelAndView.addObject("types",newTypes);
        modelAndView.addObject("tags", newTags);
        modelAndView.setViewName("index");
        return modelAndView;
    }

    /**
     * 查看某个博客的详情
     * @param id
     * @param modelAndView
     * @return
     */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam(value = "id") Integer id,
                               ModelAndView modelAndView) {
        Blog blog = blogService.findBlogById(id);
        int newView=blog.getView()+1;
        blog.setView(newView);
        //更新浏览次数
        blogService.updateView(newView,id);
        String content = MarkdownUtils.markdownToHtmlExtensions(blog.getContent());
        blog.setContent(content);
        //博客详情的标签
        List<Integer> tagIdByBlogId = tagService.findTagIdByBlogId(id);
        List<Tag> tags = new ArrayList<>();
        for (Integer i : tagIdByBlogId) {
            Tag tag = tagService.findTagById(i);
            tags.add(tag);
        }
        //博客留言
        List<Comment> comments = commentService.findCommentByBlogId(id);
        modelAndView.addObject("comments", comments);
        modelAndView.addObject("tags", tags);
        modelAndView.addObject("blog", blog);
        modelAndView.setViewName("blog");
        return modelAndView;
    }

    /**
     * 归档
     * @param modelAndView
     * @return
     */
    @GetMapping("/archive")
    public ModelAndView archive(ModelAndView modelAndView) {
        int counts = blogService.findCounts();
        List<Blog> allBlog = blogService.findAllBlog();
        modelAndView.addObject("blogs", allBlog);
        modelAndView.addObject("total", counts);
        modelAndView.setViewName("archives");
        return modelAndView;
    }


    @GetMapping("/search")
    public ModelAndView search(Integer page,Integer rows,String title,ModelAndView modelAndView) {
        page = page == null ? blogPage : page;
        rows = rows == null ? blogRows : rows;
        List<Blog> blogList = blogService.searchBlogByTitle(page, rows, title);
        Integer total = blogService.searchCount(title);
        Integer totalPages = total % rows == 0 ? total / rows : total / rows + 1;
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPage(page);
        pageInfo.setRows(rows);
        pageInfo.setCounts(total);
        pageInfo.setBlogList(blogList);
        pageInfo.setTotalPages(totalPages);
        modelAndView.addObject("pageInfo", pageInfo);
        //首页分类信息(默认从0开始查6个)
        List<Type> types = typeService.findSomeType(0, 6);
        List<Type> newTypes = new ArrayList<>();
        for (Type type:types) {
            Integer count = typeService.findTypeCountByBlog(type.getId());
            type.setTotal(count);
            newTypes.add(type);
        }
        //首页标签(默认查全部)
        List<Tag> tags = tagService.findAllTag();
        List<Tag> newTags = new ArrayList<>();
        for (Tag tag : tags) {
            Integer counts1 = tagService.findTagCounts(tag.getId());
            tag.setTotal(counts1);
            newTags.add(tag);
        }
        //首页最新博客推荐(倒叙推荐8个)
        List<Blog> newBlog = blogService.findNewBlog(newBlogStart, newBlogRows);
        modelAndView.addObject("newBlogs", newBlog);
        modelAndView.addObject("types",newTypes);
        modelAndView.addObject("tags", newTags);
        modelAndView.setViewName("index");
        return modelAndView;

    }
}
